package com.jxmcloud.business.cut.json;

import com.xlcloud.platform.util.PublicUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jxmcloud.business.cut.service.json.IConfigWheelInfoService;
import com.jxmcloud.business.cut.vo.json.ConfigWheelInfoVo;
import com.jxmcloud.business.cut.model.json.ConfigWheelInfo;

@Slf4j
@Component
public class DataConfigWheelInfoLoader {
    @Resource
    private IConfigWheelInfoService configWheelInfoService;
    //
    private Map<Integer, ConfigWheelInfoVo> configWheelInfoMap = Collections.emptyMap();

    public boolean loadFromDb() {
        try {
            Map<Integer, ConfigWheelInfoVo> tempMap = new HashMap<>();

            List<ConfigWheelInfo> list = this.configWheelInfoService.list();
            if (PublicUtil.isNotEmpty(list)) {
                List<ConfigWheelInfoVo> configWheelInfoVos = PublicUtil.copyList(list, ConfigWheelInfoVo.class);
                tempMap = configWheelInfoVos.stream().collect(Collectors.toMap(k -> k.getId(), v -> v));
            }
            
            // 创建不可修改的Map
            log.info("加载configWheelInfo表数据成功！");
            this.configWheelInfoMap = Collections.unmodifiableMap(tempMap);
            return true;
        } catch (Exception e) {
            log.error("加载数据失败！{}", e);
        }
        return false;
    }

    public Map<Integer, ConfigWheelInfoVo> getConfigWheelInfoMap() {
        return this.configWheelInfoMap; // 返回不可变Map
    }

    //
    public ConfigWheelInfoVo getConfigById(Integer id) {
        ConfigWheelInfoVo data = this.configWheelInfoMap.get(id);
        if (ObjectUtil.isNull(data)) {
            return null;
        }
        return data;
    }

    public boolean reloadFromDb() {
        this.configWheelInfoMap = new HashMap<>();
        return this.loadFromDb();
    }
}
